/*
mainwindow.cpp
(c) 2010 Yury Hamenkov
This file is part of qWordCards.

qWordCards is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

qWordCards is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with qWordCards. If not, see <http://www.gnu.org/licenses/>.
*/

#include "mainwindow.h"
#include "dictionary.h"
#include "dictionariesmanager.h"
#include "word.h"
#include <QList>
#include <QVBoxLayout>
#include <QDebug>
#include "preferences.h"
#include "wgtsettings.h"
#include <QMessageBox>
#include <QMoveEvent>
#include <QResizeEvent>
#include <QCloseEvent>
#include <QPaintEvent>
#include <QCoreApplication>
#include <QDesktopServices>
#include <QUrl>

using namespace qWordCards;

MainWindow::MainWindow() : QWidget(0, Qt::SplashScreen | Qt::WindowStaysOnTopHint /*| Qt::CustomizeWindowHint | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint*/),
	ICNTray(QPixmap(":/icons/trayicon.png")),
	ICNPlay(QPixmap(":/icons/play.png")),
	ICNPause(QPixmap(":/icons/pause.png")),
	ICNStop(QPixmap(":/icons/stop.png")),
	ICNNext(QPixmap(":/icons/next.png")),
	ICNPrev(QPixmap(":/icons/previous.png")),
	ICNSelectDictionaries(QPixmap(":/icons/books.png")),
	ICNSettings(QPixmap(":/icons/settings.png")),
	ICNRescanDictionaries(QPixmap(":/icons/update.png")),
	ICNAbout(QPixmap(":/icons/information.png")),
	ICNQuit(QPixmap(":/icons/quit.png")),
	ICNKnownWordTrue(QPixmap(":/icons/bin-full.png")),
	ICNKnownWordFalse(QPixmap(":/icons/bin.png")),
	ICNFavouriteWordTrue(QPixmap(":/icons/bookmark.png")),
	ICNFavouriteWordFalse(QPixmap(":/icons/bookmark-grey.png")),
	ICNDownloadDictionaries(QPixmap(":/icons/globe.png")),
	ICNBook(QPixmap(":/icons/book.png")),
	ICNBookQuestion(QPixmap(":/icons/book-question.png")),
	ICNCloseWindow(QPixmap(":/icons/cross.png"))
{

	Preferences::Load();
	currentState = MainWindow::None;
	currentWordIndex = -1;
	
	QObject::connect(&_manager, SIGNAL(sg_LoadingComplete()), this, SLOT(sl_DictionaryLoadingCompleted()));
	
	this->setMinimumSize(150, 150);
	
	// Timers
	this->TMRShowTimer = new QTimer(this);
	QObject::connect(this->TMRShowTimer, SIGNAL(timeout()), this, SLOT(sl_ShowTimerTimeOut()));
	
	this->TMRWordChangeTimer = new QTimer(this);
	QObject::connect(this->TMRWordChangeTimer, SIGNAL(timeout()), this, SLOT(sl_WordChangeTimerTimeOut()));
	
	this->TMRShowTranslationTimer = new QTimer(this);
	QObject::connect(this->TMRShowTranslationTimer, SIGNAL(timeout()), this, SLOT(sl_ShowTranslationTimerTimeOut()));

	// Actions
	ACTPlayPause = new QAction(ICNPlay, tr("Play"), 0);
	ACTPlayPause->setToolTip(tr("Play"));
	QObject::connect(ACTPlayPause, SIGNAL(triggered()), this, SLOT(sl_PlayPausePressed()));
	
	ACTStop = new QAction(ICNStop, tr("Stop"), 0);
	QObject::connect(ACTStop, SIGNAL(triggered()), this, SLOT(sl_StopPressed()));
	
	ACTPrevious = new QAction(ICNPrev, tr("Previous word"), 0);
	QObject::connect(ACTPrevious, SIGNAL(triggered()), this, SLOT(sl_PreviousPressed()));
	
	ACTNext = new QAction(ICNNext, tr("Next word"), 0);
	QObject::connect(ACTNext, SIGNAL(triggered()), this, SLOT(sl_NextPressed()));
	
	ACTDontShowThisWord = new QAction(ICNKnownWordFalse, tr("Don't show this word anymore"), 0);
	QObject::connect(ACTDontShowThisWord, SIGNAL(triggered()), this, SLOT(sl_DontShowThisWordPressed()));
	
	ACTMarkThisWord = new QAction(ICNFavouriteWordFalse, tr("Add to favourites"), 0);
	QObject::connect(ACTMarkThisWord, SIGNAL(triggered()), this, SLOT(sl_AddWordToFavouritesPressed()));
	
	ACTSettings = new QAction(ICNSettings, tr("Program settings"), 0);
	QObject::connect(ACTSettings, SIGNAL(triggered()), this, SLOT(sl_SettingsPressed()));
	
	ACTUpdateDictionaries = new QAction(ICNRescanDictionaries, tr("Rescan dictionaries"), 0);
	QObject::connect(ACTUpdateDictionaries, SIGNAL(triggered()), this, SLOT(sl_UpdateDictionariesPressed()));
	
	ACTDownloadDictionaries = new QAction(ICNDownloadDictionaries, tr("Download dictionaries"), 0);
	QObject::connect(ACTDownloadDictionaries, SIGNAL(triggered()), this, SLOT(sl_DownloadDictionariesPressed()));

	ACTCloseWindow = new QAction(ICNCloseWindow, tr("Close window"), 0);
	QObject::connect(ACTCloseWindow, SIGNAL(triggered()), this, SLOT(sl_CloseWindowPressed()));
	
	ACTAbout = new QAction(ICNAbout, tr("About"), 0);
	QObject::connect(ACTAbout, SIGNAL(triggered()), this, SLOT(sl_AboutPressed()));
	
	ACTQuit = new QAction(ICNQuit, tr("Quit program"), 0);
	QObject::connect(ACTQuit, SIGNAL(triggered()), this, SLOT(sl_QuitPressed()));
	
	
	//MNUDictionariesList = new QMenu(tr("Select dictionaries"));
	MNUDictionariesList = _manager.GetMenu();
	MNUDictionariesList->setIcon(ICNSelectDictionaries);
	
	MNUTrayMenu = new QMenu(this);
	MNUTrayMenu->addMenu(MNUDictionariesList);
	MNUTrayMenu->addSeparator();
	MNUTrayMenu->addAction(ACTPlayPause);
	MNUTrayMenu->addAction(ACTStop);
	MNUTrayMenu->addAction(ACTPrevious);
	MNUTrayMenu->addAction(ACTNext);
	MNUTrayMenu->addSeparator();
	MNUTrayMenu->addAction(ACTDontShowThisWord);
	MNUTrayMenu->addAction(ACTMarkThisWord);
	MNUTrayMenu->addSeparator();
	MNUTrayMenu->addAction(ACTSettings);
	MNUTrayMenu->addAction(ACTUpdateDictionaries);
	MNUTrayMenu->addAction(ACTDownloadDictionaries);
	MNUTrayMenu->addSeparator();
	MNUTrayMenu->addAction(ACTAbout);
	MNUTrayMenu->addSeparator();
	MNUTrayMenu->addAction(ACTQuit);
	
	// Tray icon
	_tray = new QSystemTrayIcon(ICNTray, this);
	_tray->setContextMenu(MNUTrayMenu);
	_tray->show();

	// Toolbar
	TBRToolBar = new QToolBar();
	TBRToolBar->setMovable(false);
	TBRToolBar->setFloatable(false);
	TBRToolBar->addAction(ACTPrevious);
	TBRToolBar->addAction(ACTPlayPause);
	TBRToolBar->addAction(ACTStop);
	TBRToolBar->addAction(ACTNext);
	TBRToolBar->addSeparator();
	TBRToolBar->addAction(ACTDontShowThisWord);
	TBRToolBar->addAction(ACTMarkThisWord);
	TBRToolBar->addSeparator();
	TBRToolBar->addAction(ACTCloseWindow);
	TBRToolBar->setMaximumHeight(30);
	
	// Text label
	LBLTextLabel = new TextLabel();
	LBLTextLabel->setWordWrap(true);
	LBLTextLabel->setMargin(5);
	LBLTextLabel->setAutoFillBackground(true);

	QVBoxLayout *l = new QVBoxLayout();
	l->setSpacing(0);
	
#if QT_VERSION >= 0x040300
	l->setContentsMargins(0, 0, 0, 0); // Introduced in Qt 4.3
#else
	l->setMargin(0);
#endif
	
	l->addWidget(TBRToolBar);
	l->addWidget(LBLTextLabel);
	setLayout(l);

	resize(Preferences::mainWindowSize);
	move(Preferences::mainWindowPosition);
	
	ApplySettings();
	sl_UpdateDictionariesPressed();
	if (Preferences::firstTime) {
		Preferences::firstTime = false;
		QMessageBox::information(this, "Information", "Thank you for using qWordCards!\nqWordCards is a simple program for stydiyng foreign language. It shows cards with foreign word and its translation.\nRead 'Readme' file for important information.");
	}
}

MainWindow::~MainWindow() {
	//foreach(Dictionary *d, this->dicts) {
	//	delete d;
	//}
}

void MainWindow::SetCurrentState(State newState) {
	this->currentState = newState;
	switch (newState) {
	case MainWindow::Playing:
		this->ACTPlayPause->setIcon(this->ICNPause);
		this->ACTPlayPause->setText(tr("Pause"));
		this->ACTStop->setEnabled(true);
		this->ACTNext->setEnabled(true);
		this->ACTPrevious->setEnabled(true);
		this->ACTUpdateDictionaries->setEnabled(true);
		this->ACTDontShowThisWord->setEnabled(true);
		this->ACTMarkThisWord->setEnabled(true);
		this->MNUDictionariesList->setEnabled(true);

		this->TMRWordChangeTimer->start();
		break;

	case MainWindow::Paused:
		this->ACTPlayPause->setIcon(this->ICNPlay);
		this->ACTPlayPause->setText(tr("Play"));

		this->ACTStop->setEnabled(true);
		this->ACTNext->setEnabled(true);
		this->ACTPrevious->setEnabled(true);
		this->ACTUpdateDictionaries->setEnabled(true);
		this->ACTDontShowThisWord->setEnabled(true);
		this->ACTMarkThisWord->setEnabled(true);
		this->MNUDictionariesList->setEnabled(true);

		this->TMRWordChangeTimer->stop();
		this->TMRShowTimer->stop();
		this->TMRShowTranslationTimer->stop();
		this->_tray->setToolTip( "qWordCards [Paused]");
		break;

	case MainWindow::Stopped:
		this->playing = false;
		this->ACTPlayPause->setIcon(this->ICNPlay);
		this->ACTPlayPause->setText(tr("Play"));
		this->ACTPlayPause->setEnabled(true);
		this->ACTStop->setEnabled(false);
		this->ACTNext->setEnabled(false);
		this->ACTPrevious->setEnabled(false);
		this->ACTUpdateDictionaries->setEnabled(true);
		this->ACTDontShowThisWord->setEnabled(false);
		this->ACTMarkThisWord->setEnabled(false);
		this->MNUDictionariesList->setEnabled(true);

		this->TMRWordChangeTimer->stop();
		this->TMRShowTimer->stop();
		this->TMRShowTranslationTimer->stop();
		this->hide();
		this->_tray->setToolTip( "qWordCards [Stopped]");
		break;

	case MainWindow::Scan:
		this->playing = false;
		this->ACTPlayPause->setIcon(this->ICNPlay);
		this->ACTPlayPause->setText(tr("Play"));
		this->ACTPlayPause->setEnabled(false);
		this->ACTStop->setEnabled(false);
		this->ACTPrevious->setEnabled(false);
		this->ACTNext->setEnabled(false);
		this->ACTUpdateDictionaries->setEnabled(false);
		this->ACTDontShowThisWord->setEnabled(false);
		this->ACTMarkThisWord->setEnabled(false);
		this->MNUDictionariesList->setEnabled(false);

		this->TMRWordChangeTimer->stop();
		this->TMRShowTimer->stop();
		this->TMRShowTranslationTimer->stop();
		this->hide();
		this->_tray->setToolTip( "qWordCards [Dictioneries are loading...]");
		break;

	case MainWindow::None:
		this->playing = false;
		this->ACTPlayPause->setIcon(this->ICNPlay);
		this->ACTPlayPause->setText(tr("Play"));
		this->ACTPlayPause->setEnabled(false);
		this->ACTStop->setEnabled(false);
		this->ACTPrevious->setEnabled(false);
		this->ACTNext->setEnabled(false);
		this->ACTUpdateDictionaries->setEnabled(true);
		this->ACTDontShowThisWord->setEnabled(false);
		this->ACTMarkThisWord->setEnabled(false);
		this->MNUDictionariesList->setEnabled(false);
		break;

	}
}

void MainWindow::sl_PlayPausePressed() {
	if (this->currentState == MainWindow::Playing) {
		SetCurrentState(MainWindow::Paused);
	} else if (this->currentState == MainWindow::Paused || this->currentState == MainWindow::Stopped) {
		SetCurrentState(MainWindow::Playing);
		sl_NextPressed();
	}
}

void MainWindow::sl_StopPressed() {
	SetCurrentState(MainWindow::Stopped);
}

void MainWindow::sl_PreviousPressed() {
	if (!PreviousWord()) {
		return;
	}
	
	if (Preferences::showTranslationInterval > 0) {
		this->LBLTextLabel->UpdateContent(this->wordsQueue[this->currentWordIndex], false);
		this->TMRShowTranslationTimer->start();
	} else {
		this->LBLTextLabel->UpdateContent(this->wordsQueue[this->currentWordIndex], true);
	}

	if (this->isHidden()) {
		this->show();
	}
	
	if (Preferences::wordShowDuration > 0) {
		this->TMRShowTimer->start();
	}
}

void MainWindow::sl_NextPressed() {
	if (!NextWord()) {
		return;
	}
	
	if (Preferences::showTranslationInterval > 0) {
		this->LBLTextLabel->UpdateContent(this->wordsQueue[this->currentWordIndex], false);
		this->TMRShowTranslationTimer->start();
	} else {
		this->LBLTextLabel->UpdateContent(this->wordsQueue[this->currentWordIndex], true);
	}

	if (this->isHidden()) {
		this->show();
	}
	
	if (Preferences::wordShowDuration > 0) {
		this->TMRShowTimer->start();
	}
	
	
	if (this->TMRWordChangeTimer->isActive()) {
		this->TMRWordChangeTimer->start();
	}
}

void MainWindow::PrintWord (bool translation) {
	
}

bool MainWindow::NextWord() {
	if (this->wordsQueue.isEmpty() ||
		(!this->wordsQueue.isEmpty() && this->currentWordIndex == this->wordsQueue.size() -1)) {
		
		// Check if at least one dictionary is active
		QMap <QString, bool> activity = _manager.GetDictionariesActivity();
		bool atLeastOneDictionaryActive = false;
		foreach(QString s, activity.keys()) {
			if (activity[s] == true) {
				atLeastOneDictionaryActive = true;
				break;
			}
		}
		if (!atLeastOneDictionaryActive) {
			SetCurrentState(MainWindow::Stopped);
			_tray->showMessage("qWordCards", tr("No dictionaries to use"), QSystemTrayIcon::NoIcon, 3000);
			return false;
		}
		
		int rnd = 0;
		rnd = qrand() % 1;
		
		if (_manager.FavouriteWordsCount() == 0) {
			rnd = 1;
		}
		
		Word w;
		
		if (rnd == 0) {
			w = _manager.GetWord(Favourite);
		} else if (rnd == 1) {
			w = _manager.GetWord(Ordinal);
		}
		//Q_ASSERT(w != 0);
		
		if (this->wordsQueue.size() == Preferences::wordsQueueLength && this->wordsQueue.size() > 0) {
			_manager.SetSuspended(wordsQueue.front().GetWord(), false); // remove word from 'suspended' queue
			this->wordsQueue.pop_front();
		}
		
		_manager.SetSuspended(w.GetWord(), true);
		this->wordsQueue.push_back(w);
		this->currentWordIndex = this->wordsQueue.size() - 1;
		
	} else {
		this->currentWordIndex++;
	}

	UpdateWordStatus();
	return true;
}
	
bool MainWindow::PreviousWord() {
	if (!this->wordsQueue.isEmpty() && this->currentWordIndex > 0) {
		this->currentWordIndex--;
	}

	UpdateWordStatus();
	return true;
}

void MainWindow::sl_QuitPressed() {
	Preferences::Save();
	QCoreApplication::quit();
}

void MainWindow::sl_SettingsPressed() {
	WGTSettings* w = new WGTSettings(0);
	w->exec();
	delete w;
	ApplySettings();
}

void MainWindow::sl_UpdateDictionariesPressed() {
	SetCurrentState(MainWindow::Scan);
	
	if (!QDir("./dicts").exists()) {
		_tray->showMessage("Information", "Directory \"./dicts\" not found!", QSystemTrayIcon::Warning);
		SetCurrentState(MainWindow::None);
		_tray->setToolTip("qWordCards [No dictionaries found]");
		return;
	}
	
	_manager.ReloadDictionaries();
	this->_tray->showMessage("qWordCards", tr("Dictionaries are loading..."),QSystemTrayIcon::NoIcon, 3000);
}

void MainWindow::sl_DictionaryLoadingCompleted() {
	qDebug() << "Loading completed";
	
	//Preferences::dicts = this->dicts;

	//this->MNUDictionariesList->clear();

	if (_manager.DictionariesCount() == 0) {
		SetCurrentState(MainWindow::None);
		_tray->setToolTip("qWordCards [No dictionaries found]");
		_tray->showMessage("qWordCards", tr("Scan completed, no dictionaries found :("),QSystemTrayIcon::NoIcon, 3000);
	} else {
		SetCurrentState(MainWindow::Stopped);
		_tray->setToolTip(QString("qWordCards [Dictionaries loaded: %1, words: %2]").arg(_manager.DictionariesCount()).arg(_manager.WordsCount()));

		

		_tray->showMessage("qWordCards", tr("Dictionaries loading complete. Loaded %1 dictionaries.").arg(_manager.DictionariesCount()),QSystemTrayIcon::NoIcon, 3000);
	}
}

void MainWindow::sl_AboutPressed() {
	QMessageBox::information(this, "About qWordCards", "qWordCards is a simple program which shows a word and it translation in an OSD window.\nProgram uses StarDict dictionaries.\n\nCode by: Yury Hamenkov (mailto:waltersullivan.11121@gmail.com)\nIcons:\nFugue Icons pack. Copyright (C) 2010 Yusuke Kamiyamane (p@yusukekamiyamane.com). All rights reserved.\nThe icons are licensed under a Creative Commons Attribution  3.0 license. <http://creativecommons.org/licenses/by/3.0/>");
}



void MainWindow::sl_ShowTimerTimeOut() {
	this->hide();
	this->TMRShowTimer->stop();
}

void MainWindow::sl_WordChangeTimerTimeOut() {
	if (!NextWord()) {
		return;
	}
	
	if (Preferences::showTranslationInterval > 0) {
		this->LBLTextLabel->UpdateContent(this->wordsQueue[this->currentWordIndex], false);
		this->TMRShowTranslationTimer->start();
	} else {
		this->LBLTextLabel->UpdateContent(this->wordsQueue[this->currentWordIndex], true);
	}
	
	if (this->isHidden()) {
		this->show();
	}
	if (Preferences::wordShowDuration > 0) {
		this->TMRShowTimer->start();
	}
}

void MainWindow::sl_ShowTranslationTimerTimeOut() {
	this->LBLTextLabel->UpdateContent(this->wordsQueue[this->currentWordIndex], true);
	this->TMRShowTranslationTimer->stop();
}

void MainWindow::sl_DontShowThisWordPressed() {
	/*
	QString word = this->wordsQueue[this->currentWordIndex].GetWord();
	
	if (Preferences::knownWords.contains(word)) {
		Preferences::knownWords.removeAll(word);
		foreach (Dictionary* d, this->dicts) {
			if (!d->IsLoaded()) {continue;}
			QMap <QString, Word>::iterator i = d->knownWords.find(word);
			if (i != d->knownWords.end()) {
				d->words.insert(word, d->knownWords.take(word));
			}
		}
	} else {
		// remove this word from favourites automatically
		if (Preferences::favouriteWords.contains(word)) {
			Preferences::favouriteWords.removeAll(word);
			foreach (Dictionary* d, this->dicts) {
				if (!d->IsLoaded()) {continue;}
				QMap <QString, Word>::iterator i = d->favouriteWords.find(word);
				if (i != d->favouriteWords.end()) {
					d->words.insert(word, d->favouriteWords.take(word));
				}
			}
		}
		
		Preferences::knownWords.append(word);
		foreach (Dictionary* d, this->dicts) {
			if (!d->IsLoaded()) {continue;}
			QMap <QString, Word>::iterator i = d->words.find(word);
			if (i != d->words.end()) {
				d->knownWords.insert(word, d->words.take(word));
			}
		}
	}
	UpdateWordStatus();
	*/
}

void MainWindow::sl_AddWordToFavouritesPressed() {
	/*
	QString word = this->wordsQueue[this->currentWordIndex].GetWord();
	if (Preferences::knownWords.contains(word)) {
		return;
	}
	
	if (Preferences::favouriteWords.contains(word)) {
		Preferences::favouriteWords.removeAll(word);
		foreach (Dictionary* d, this->dicts) {
			if (!d->IsLoaded()) {continue;}
			QMap <QString, Word>::iterator i = d->favouriteWords.find(word);
			if (i != d->favouriteWords.end()) {
				d->words.insert(word, d->favouriteWords.take(word));
			}
		}
	} else {
		Preferences::favouriteWords.append(word);
		foreach (Dictionary* d, this->dicts) {
			if (!d->IsLoaded()) {continue;}
			QMap <QString, Word>::iterator i = d->words.find(word);
			if (i != d->words.end()) {
				d->favouriteWords.insert(word, d->words.take(word));
			}
		}
	}
	UpdateWordStatus();
	*/
}

void MainWindow::UpdateWordStatus() {
	if (Preferences::forbiddenWords.Contains(this->wordsQueue[this->currentWordIndex].GetWord())) {
		this->ACTDontShowThisWord->setIcon(this->ICNKnownWordTrue);
	} else {
		this->ACTDontShowThisWord->setIcon(this->ICNKnownWordFalse);
	}

	if (Preferences::favouriteWords.Contains(this->wordsQueue[this->currentWordIndex].GetWord())) {
		this->ACTMarkThisWord->setIcon(this->ICNFavouriteWordTrue);
	} else {
		this->ACTMarkThisWord->setIcon(this->ICNFavouriteWordFalse);
	}
}

void MainWindow::sl_DownloadDictionariesPressed() {
	QDesktopServices::openUrl(QUrl("http://xdxf.revdanica.com/down/index.php"));
}

void MainWindow::sl_CloseWindowPressed() {
	SetCurrentState(MainWindow::Stopped);
	hide();
}

void MainWindow::ApplySettings() {
	this->LBLTextLabel->UpdatePalette();
	this->TMRShowTimer->setInterval(Preferences::wordShowDuration);
	this->TMRWordChangeTimer->setInterval(Preferences::wordChangeInterval);
	this->TMRShowTranslationTimer->setInterval(Preferences::showTranslationInterval);
	this->TBRToolBar->setVisible(Preferences::showToolbar);
}

/* virtual */
void MainWindow::moveEvent ( QMoveEvent * event ) {
	QWidget::moveEvent(event);
	Preferences::mainWindowPosition = event->pos();
}

/* virtual */
void MainWindow::mouseMoveEvent ( QMouseEvent * event ) {
	QWidget::mouseMoveEvent(event);
	if (mousePressed && (this->pressedButton == Qt::LeftButton || this->pressedButton == Qt::RightButton)) {
		QPoint p = event->globalPos();
		int dx = p.x() - mousePos.x();
		int dy = p.y() - mousePos.y();
		if (this->pressedButton == Qt::LeftButton) {
			p.setX(this->pos().x() + dx);
			p.setY(this->pos().y() + dy);
			this->move(p);
		} else if (this->pressedButton == Qt::RightButton) {
			QSize s(this->size().width() + dx, this->size().height() + dy);
			if (s.width() <= 500 && s.height() <= 500) {
				this->resize(s);
			}
		}
		mousePos = event->globalPos();
	}
}

/* virtual */
void MainWindow::mousePressEvent ( QMouseEvent * event ) {
	QWidget::mousePressEvent(event);
	this->mousePressed = true;
	this->pressedButton = event->button();
	mousePos = event->globalPos();
}

/* virtual */
void MainWindow::mouseReleaseEvent ( QMouseEvent * event ) {
	QWidget::mouseReleaseEvent(event);
	if (mousePressed) {
		this->mousePressed = false;
	}
}

/* virtual */
void MainWindow::resizeEvent ( QResizeEvent * event ) {
	QWidget::resizeEvent(event);
	Preferences::mainWindowSize = event->size();
}

/* virtual */
void MainWindow::closeEvent ( QCloseEvent * event ) {
	event->ignore();
	SetCurrentState(MainWindow::Stopped);
}


